from orator import DatabaseManager
from orator import Model

DATA_PHONE = {
    'mysql': {
        'driver': 'mysql',
        'host': '****',
        'database': '*',
        'user': '*',
        'password': '*',
        'prefix': ''
    }
}

db = DatabaseManager(DATA_PHONE)
Model.set_connection_resolver(db)

class Phone(Model) :
    __table__ = 'phoneproxy'
    __primary_key__ = 'id'
    __timestamps__ = True

    @staticmethod
    def insert(skuid, category, url, skuname, skushop, detail, comment) :
        old = Phone.where('skuid', skuid).first()

        if old is None :
            one = Phone()
            one.skuid = skuid
            one.category = category
            one.url = url
            one.skuname = skuname
            one.skushop = skushop
            one.detail = detail
            one.comment = comment
            return one.save()
        else :
            return Phone.where('skuid', skuid).update(category = category, url = url, skuname = skuname, skushop = skushop, detail = detail, comment = comment)

    @staticmethod
    def getnullprice() :
        return Phone.where('price', 0).first()

    @staticmethod
    def updateprice(skuid, price) :
        return Phone.where('skuid', skuid).update(price=price)

    @staticmethod
    def isExisted(skuid):
        return Phone.where('skuid', skuid).first() != None